﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;

namespace QASampling.BusinessObject
{
    class QAController
    {
        private static string username;
        public static string Username
        {
            get { return username; }
            set { username = value; }
        }

       // private static string docid;
        //public static string DocId
        //{
          //  get { return docid; }
           // set { docid = value; }
        //}

        private static bool readOnly;
        public static bool ReadOnly
        {
            get { return readOnly; }
            set { readOnly = value; }
        }

        private static TipeSPT tipeSPT;
        public static TipeSPT TipeSPT
        {
            get { return tipeSPT; }
            set { tipeSPT = value; }
        }

        private static IList<QAFieldErrorMsg> fieldErrorMsg;
        public static IList<QAFieldErrorMsg> FieldErrorMsg
        {
            get { return fieldErrorMsg; }
            set { fieldErrorMsg = value; }
        }

        private static QAJob job;
        public static QAJob Job
        {
            get { return job; }
            set { job = value; }
        }

        internal static IList<QAJob> GetAllJob()
        {
            return DAO.GetAllJob();
        }

        internal static IList<QAJob> GetJobs(string username)
        {
            return DAO.GetJob(username);
        }

        internal static int GetDocIDFromStellent(string barcode, string npwp, string namawp)
        {
            return DAO.GetDocIDFromStellent(barcode, npwp, namawp);
        }

        internal static QAJob GetJobByDocId(string docid)
        {
            return DAO.GetJobByDocId(docid);
        }

        internal static QAJob GetJobByDocIdAndBarcode(string docid, string barcode)
        {
            return DAO.GetJobByDocIdAndBarcode(docid, barcode);
        }       

        internal static QAJob GetLastJob()
        {
            return DAO.GetLastJob(username);
        }

        internal static void UpdateForm1111DM(Form1111DM form1111DM, string log)
        {
            string statusAkhir = log.Length == 0 ? "no corrections" : "corrected";

            String tmpLog;
            if (log.Length == 0)
                tmpLog = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + " by " + Username + Environment.NewLine + statusAkhir + Environment.NewLine + Environment.NewLine;
            else
                tmpLog = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + " by " + Username + Environment.NewLine + log + Environment.NewLine;

            QAJob job = DAO.GetJobByDocId(form1111DM.DocId);
            string originalLog = job.Catatan;
            DAO.UpdateForm1111DM(form1111DM, tmpLog, originalLog, statusAkhir);
        }

        internal static void UpdateForm1111(Form1111 form1111, string log)
        {
            string statusAkhir = log.Length == 0 ? "no corrections" : "corrected";

            String tmpLog;
            if (log.Length == 0)
                tmpLog = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + " by " + Username + Environment.NewLine + statusAkhir + Environment.NewLine + Environment.NewLine;
            else
                tmpLog = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + " by " + Username + Environment.NewLine + log + Environment.NewLine;

            QAJob job = DAO.GetJobByDocId(form1111.DocId);
            string originalLog = job.Catatan;
            DAO.UpdateForm1111(form1111, tmpLog, originalLog, statusAkhir);
        }

        internal static bool Login(string username, string password)
        {
            if (AD.ValidateUserWithAD(username, password))
            {
                if (DAO.IsValidLogin(username))
                {
                    tipeSPT = DAO.GetTipeSPTFromUser(username);
                    return true;
                }
                else return false;
            }
            else return false;
        }

        internal static string[] GetAlasanQA()
        {
            string[] alasanQA;
            try
            {
                alasanQA = ConfigurationManager.AppSettings["AlasanQA"].Split(',');
            }
            catch (Exception)
            {
                throw new Exception("AlasanQA belum dibuat di .config");
            }
            return alasanQA;
        }

        internal static void UpdateField(string fieldname, string fieldvalue, string table)
        {
            DAO.UpdateField(fieldname, fieldvalue, table, job.DocId);
        }

        internal static void UpdateQAJob(string log, string statusAkhir)
        {
            log = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss ") + log + " oleh " + username + Environment.NewLine;
           // QAJob job = DAO.GetJobByDocId(docid);
            string originalLog = job.Catatan;
            DAO.UpdateQAJob(log, originalLog, statusAkhir, job.DocId, job.Barcode);
        }

        internal static void UbahStatusLoginPetugas(int islogin)
        {
            DAO.UbahStatusLoginPetugas(username, islogin);
        }

        internal static void TambahQALog(QAChangeLog qa)
        {
            qa.DocId = job.DocId;
            qa.Barcode = job.Barcode;
            qa.Editor = username;
            DAO.TambahQAChangeLog(qa);
        }

        internal static void TambahField(string[] fieldname, string[] fieldvalue, string table, string tableNo)
        {
            DAO.TambahField(fieldname, fieldvalue, table, job.DocId, tableNo);
        }

        internal static void HapusField(string[] fieldname, string[] fieldvalue, string table)
        {
            DAO.HapusField(fieldname, fieldvalue, table, job.DocId);
        }

        internal static void EditField(string[] fieldname, string[] fieldvalue, string table, string id)
        {
            DAO.EditField(fieldname, fieldvalue, table, job.DocId, id);
        }

        internal static DataTable GetSPTWithFieldForGrid(string tableSource, string[] fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["Datacap"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, fieldName, job.DocId);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static DataTable GetSPTWithFieldFor1108(string tableSource, string[] fieldName, string tableNo)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["Datacap"].ConnectionString)))
                {
                    return DAO.GetTable1108(con, tableSource, fieldName, job.DocId, tableNo);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static DataTable GetSPTWithField(string tableSource, string[] fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["Datacap"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, fieldName, job.DocId, job.Barcode);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static DataTable GetSPTWithField(string tableSource, string fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["Datacap"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, new string[] { fieldName }, job.DocId, job.Barcode);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static DataTable GetQAChangeLog(string tableSource, string fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["QA"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, new string[] { fieldName }, job.DocId, job.Barcode);
                }                
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static DataTable GetQAChangeLog(string tableSource, string[] fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["QA"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, fieldName, job.DocId, job.Barcode);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        //added by Vincent for validation login to qa sampling not for admin and supervisor 20130201
        internal static bool IsSupervisor(string username)
        {
            return DAO.IsValidSupervisor(username);
        }

        internal static bool IsAdmin(string username)
        {
            return DAO.IsValidAdminLogin(username);
        }
        //-------------------------------------------------------------------------------------------
    }
}
